using System;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace Microsoft.Cryptography
{
    /// <summary>
    /// Enumerator for different RSA-signature algorithms
    /// </summary>
    public enum SignatureAlgorithms
    {
        /// <summary>
        /// Unknown
        /// </summary>
        Unknown = 0,
        /// <summary>
        /// RSAEncryption
        /// </summary>
        RSAEncryption = 1,
        // Not Supported
        //MD2WithRSAEncryption = 2,
        /// <summary>
        /// MD5WithRSAEncryption
        /// </summary>
        MD5WithRSAEncryption = 4,
        /// <summary>
        /// SHA1WithRSAEncryption
        /// </summary>
        SHA1WithRSAEncryption = 5,
        // Not Supported
        //SHA224WithRSAEncryption = 14,
        /// <summary>
        /// SHA256WithRSAEncryption
        /// </summary>
        SHA256WithRSAEncryption = 11,
        /// <summary>
        /// SHA384WithRSAEncryption
        /// </summary>
        SHA384WithRSAEncryption = 12,
        /// <summary>
        /// SHA512WithRSAEncryption
        /// </summary>
        SHA512WithRSAEncryption = 13,
        /// <summary>
        /// RipeMD160WithRSAEncryption
        /// </summary>
        RipeMD160WithRSAEncryption = 102,
        // Not Supported
        //RipeMD128WithRSAEncryption = 103,
        // Not Supported
        //RipeMD256WithRSAEncryption = 104
    }

    /// <summary>
    /// Helper class for converting signature algorithm name to teh corresponding hash algorithm names
    /// </summary>
    public static class CryptoConfigHelper
    {

        /// <summary>
        /// Function for converting signature algorithm name to teh corresponding hash algorithm names
        /// </summary>
        /// <param name="signatureAlgorithm">signature algorithm name</param>
        /// <returns>Corresponding hash algorithm name</returns>
        public static HashAlgorithm GetHashAlgorithm(SignatureAlgorithms signatureAlgorithm)
        {
            HashAlgorithm alg = null;
            switch (signatureAlgorithm)
            {
                // Not supported
                //case SignatureAlgorithms.MD2WithRSAEncryption:
                //    alg = HashAlgorithm.Create("MD2");
                //    break;
                case SignatureAlgorithms.MD5WithRSAEncryption:
                    alg = HashAlgorithm.Create("MD5");
                    break;
                // Not supported
                //case SignatureAlgorithms.RipeMD128WithRSAEncryption:
                //    alg = HashAlgorithm.Create("RipeMD128");
                //    break;
                case SignatureAlgorithms.RipeMD160WithRSAEncryption:
                    alg = HashAlgorithm.Create("RipeMD160");
                    break;
                // Not supported
                //case SignatureAlgorithms.RipeMD256WithRSAEncryption:
                //    alg = HashAlgorithm.Create("RipeMD256");
                //    break;
                case SignatureAlgorithms.SHA1WithRSAEncryption:
                    alg = HashAlgorithm.Create("SHA1");
                    break;
                // Not supported
                //case SignatureAlgorithms.SHA224WithRSAEncryption:
                //    alg = HashAlgorithm.Create("SHA224");
                //    break;
                case SignatureAlgorithms.SHA256WithRSAEncryption:
                    alg = HashAlgorithm.Create("SHA256");
                    break;
                case SignatureAlgorithms.SHA384WithRSAEncryption:
                    alg = HashAlgorithm.Create("SHA384");
                    break;
                case SignatureAlgorithms.SHA512WithRSAEncryption:
                    alg = HashAlgorithm.Create("SHA512");
                    break;
            }

            if (alg == null)
            {
                throw new Exception(string.Format("No Hash-Algorithm found for SignatureAlgorithm '{0}'.",
                    signatureAlgorithm));
            }

            return alg;
        }
    }
}
